﻿//Import libraries
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DynamicSQLGenerator
{
    public partial class frmMain : Form
    {
        //Reset original text
        String TSQLOriginal = "";

        public frmMain()
        {
            //Initialize form
            InitializeComponent();
            mnMainMenu.BackColor = Color.LightYellow;

        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            //Check if the text box is empty
            if (!txtTSQL.Text.Equals(""))
            {
                TSQLOriginal = txtTSQL.Text;

                String TSQLDynamic = TSQLOriginal;

                if (((txtTSQL.Text.StartsWith("'")) && (txtTSQL.Text.EndsWith("'"))))
                    TSQLDynamic = TSQLDynamic.Substring(1, TSQLDynamic.Length - 2);

                //Make T-SQL dynamic
                TSQLDynamic = TSQLDynamic.Replace("'", "''");

                //Add single quotes in the beggining and the end of the T-SQL statement (if not exist already)
                TSQLDynamic = "'" + TSQLDynamic;
                TSQLDynamic = TSQLDynamic + "'";

                txtTSQL.Clear();                

                txtTSQL.Text = TSQLDynamic;
            }
        }

        private void btnOriginalText_Click(object sender, EventArgs e)
        {
            txtTSQL.Text = TSQLOriginal;
        }

        private void btnGenerateStaticSQL_Click(object sender, EventArgs e)
        {

            //Check if the text box is empty
            if (!txtTSQL.Text.Equals(""))
            {
                TSQLOriginal = txtTSQL.Text;

                String TSQLStatic = TSQLOriginal.Replace("''", "'");

                if (((txtTSQL.Text.StartsWith("'")) && (txtTSQL.Text.EndsWith("'"))))
                    TSQLStatic = TSQLStatic.Substring(1, TSQLStatic.Length - 2);

                txtTSQL.Text = TSQLStatic;
            }
        }

        private void btnCopyToClipboard_Click(object sender, EventArgs e)
        {
            if (!txtTSQL.Text.Equals(""))
                Clipboard.SetText(txtTSQL.Text);
        }

        private void btnPasteToClipboard_Click(object sender, EventArgs e)
        {
            txtTSQL.Clear();
            txtTSQL.Text = Clipboard.GetText();

            TSQLOriginal = txtTSQL.Text;

        }

        private void btnClearTextBox_Click(object sender, EventArgs e)
        {
            txtTSQL.Clear();
            TSQLOriginal = "";

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnToUpperCase_Click(object sender, EventArgs e)
        {
            TSQLOriginal = txtTSQL.Text;

            txtTSQL.Text = txtTSQL.Text.ToUpper();
        }

        private void btnToLowerCase_Click(object sender, EventArgs e)
        {
            TSQLOriginal = txtTSQL.Text;

            txtTSQL.Text = txtTSQL.Text.ToLower();
        }

        private void mnItemExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


        private void mnItemAbout_Click_1(object sender, EventArgs e)
        {
            String about = "";
            about += "This utility generates dynamic T-SQL (and vice versa) statements based on the query the user inputs.";
            about += Environment.NewLine;
            about += Environment.NewLine;
            about += "Disclaimer: This software is under the Microsoft Public License (Ms-PL). The author of this program will not be responsible for illegal actions or damage caused by the misuse of this software.";
            about += Environment.NewLine;
            about += Environment.NewLine;
            about += "Author's Email Address: a_artemiou@hotmail.com";
            about += Environment.NewLine;
            about += Environment.NewLine;
            about += "Project's website: http://dynamicsqlgen.codeplex.com";

            MessageBox.Show(this, about, "About this Program", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
    }
}
